﻿using System.Collections.Generic;
using System.Linq;

namespace RNA_Libraries
{
  public class MutationCollection : HashSet<IMutation>
  {
    private readonly Sequence m_parent;
    private readonly Vertex m_read;

    public MutationCollection(Sequence parent, IEnumerable<IMutation> muts, Vertex read = null)
      : base(muts)
    {
      m_parent = parent;
      m_read = read;
    }

    public Vertex Read { get { return m_read; } }

    public Sequence Mutate
    {
      get
      {
        var chars = m_parent.ToString().ToList();
        var deleted = 0;
        foreach (var mutation in this)
        {
          string mut;
          switch (mutation.MutationType)
          {
            case MutationType.Replace:
              mut = ((ReplaceMutation)mutation).Symbol;
              chars.RemoveAt(mutation.Position - deleted);
              chars.InsertRange(mutation.Position - deleted, mut);
              deleted -= mut.Length - 1;
              break;
            case MutationType.Insert:
              mut = ((InsertMutation)mutation).Symbol;
              chars.InsertRange(mutation.Position - deleted, mut);
              deleted -= mut.Length;
              break;
            case MutationType.Delete:
              chars.RemoveAt(mutation.Position - deleted);
              deleted++;
              break;
          }
        }
        return new Sequence(new string(chars.ToArray()));
      }
    }
  }
}
